package com.zzyl.nursing.service.impl;

import java.util.HashMap;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.service.WechatService;
import com.zzyl.nursing.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.Map;

/**
 * 老人家属Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-05
 */
@Service
public class FamilyMemberServiceImpl extends ServiceImpl<FamilyMemberMapper, FamilyMember> implements IFamilyMemberService
{
    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    @Autowired
    private WechatService wechatService;

    @Autowired
    private TokenService tokenService;


    /**
     * 查询老人家属
     * 
     * @param id 老人家属主键
     * @return 老人家属
     */
    @Override
    public FamilyMember selectFamilyMemberById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询老人家属列表
     * 
     * @param familyMember 老人家属
     * @return 老人家属
     */
    @Override
    public List<FamilyMember> selectFamilyMemberList(FamilyMember familyMember)
    {
        return familyMemberMapper.selectFamilyMemberList(familyMember);
    }

    /**
     * 新增老人家属
     * 
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int insertFamilyMember(FamilyMember familyMember)
    {
        return save(familyMember) ? 1 : 0;
    }

    /**
     * 修改老人家属
     * 
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int updateFamilyMember(FamilyMember familyMember)
    {
        return updateById(familyMember) ? 1 : 0;
    }

    /**
     * 批量删除老人家属
     * 
     * @param ids 需要删除的老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除老人家属信息
     * 
     * @param id 老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public LoginVo login(UserLoginRequestDto userLoginRequestDto) {
        //1.远程调用appid+appsecret+code ----->openid
        String openId = wechatService.getOpenId(userLoginRequestDto.getCode());
        
        //2.openid 去数据库查询是否有用户登录过zzyl小程序
        LambdaQueryWrapper<FamilyMember> familyMemberLambdaQueryWrapper = new LambdaQueryWrapper<>();
        familyMemberLambdaQueryWrapper.eq(FamilyMember::getOpenId,openId);
        FamilyMember familyMember = this.getOne(familyMemberLambdaQueryWrapper);
        //2.1 远程调用appid+appsecret+phonecode 去微信官方查询手机号
        String phone = wechatService.getPhone(userLoginRequestDto.getPhoneCode());

        //4.有值，更新数据，微信官方查询手机号和你远程数据库手机号是否相
        if(familyMember==null){
            familyMember = new FamilyMember();
            familyMember.setOpenId(openId);
            familyMember.setPhone(phone);
            familyMember.setGender(0);
            familyMember.setName("蒋玉登666");
            this.save(familyMember);
        }else if(phone!=null && !phone.equals(familyMember.getPhone())){
            UpdateWrapper<FamilyMember> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("phone",phone).eq("openId",familyMember.getOpenId());
            this.update(updateWrapper);
        }
        //5.userid jwt 进行加密放回
        Map<String,Object> map = new HashMap<>();
        map.put("userId",familyMember.getId());
        map.put("nickName",familyMember.getName());
        String token = tokenService.createToken(map);

        LoginVo loginVo = new LoginVo();
        loginVo.setToken(token);
        loginVo.setNickName(familyMember.getName());
        return loginVo;
    }


}
